package com.ruoyi.api.rabbitMQ;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ywh
 * @Date 2020-11-26
 **/
//@Configuration
public class RabbitmqConfig {
    /*队列名称*/
    public static final String QUEUE_PAY = "queue_pay";
    /*交换机名称*/
    public static final String ORDER_DELAY_EXCHANGE="order_delay_exchange";
    /*路由*/
    public static final String ROUTINGKEY="order_route";


    //声明交换机
    @Bean(ORDER_DELAY_EXCHANGE)
    public Exchange delayExchange(){
        //durable(true) 持久化，mq重启之后交换机还在
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(ORDER_DELAY_EXCHANGE,"x-delayed-message",true, false,args);
    }

    //声明队列
    @Bean(QUEUE_PAY)
    public Queue delayQueue(){
        return new Queue(QUEUE_PAY,true);
    }

    //队列绑定交换机，指定routingKey
    @Bean()
    public Binding cfgDelayBinding(@Qualifier(QUEUE_PAY) Queue queue,
                                              @Qualifier(ORDER_DELAY_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY).noargs();
    }


}
